package jinyilw.swing.rlaf.ui;

import jinyilw.swing.FontTools;
import jinyilw.swing.ScreenTools;
import jinyilw.swing.graphics.RenderingConfig;
import jinyilw.swing.image.ImageTools;
import jinyilw.swing.rlaf.theme.RThemeColor;
import jinyilw.swing.rlaf.utils.RBorderFactory.EmptyBorder;
import jinyilw.swing.visuals.Icomp.IPopupMenu;

import javax.swing.BoxLayout;
import javax.swing.JComponent;
import javax.swing.JPopupMenu;
import javax.swing.LookAndFeel;
import javax.swing.Popup;
import javax.swing.border.Border;
import javax.swing.plaf.ComponentUI;
import javax.swing.plaf.UIResource;
import javax.swing.plaf.basic.BasicPopupMenuUI;
import javax.swing.plaf.basic.DefaultMenuLayout;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.Shape;
import java.awt.geom.RoundRectangle2D.Float;

/**
 * @author Jinyi
 */
public class RPopupMenuUI extends BasicPopupMenuUI
{
	private static final int arc = 7;
	private static final Border border = new EmptyBorder(arc / 2, 1, arc / 2, 1);

	private Image blurImage, clearImage;
	private int fontIdentifier = FontTools.fontIdentifier;

	public static ComponentUI createUI(JComponent c)
	{
		return new RPopupMenuUI();
	}

	@Override
	public void installUI(JComponent c)
	{
		super.installUI(c);
		popupMenu.setBorderPainted(false);
		popupMenu.setLightWeightPopupEnabled(true);
		popupMenu.setBorder(border);
	}

	@Override
	public void installDefaults()
	{
		if (popupMenu.getLayout() == null
				|| popupMenu.getLayout() instanceof UIResource)
			popupMenu.setLayout(
					new DefaultMenuLayout(popupMenu, BoxLayout.Y_AXIS));

		LookAndFeel.installProperty(popupMenu, "opaque", Boolean.FALSE);
	}

	@Override
	public void update(Graphics g, JComponent c)
	{
		int h = c.getHeight();
		int w = c.getWidth();
		Graphics2D g2d = (Graphics2D) g;
		RenderingConfig oldRenderingHints = RenderingConfig
				.enableAAPainting(g2d);
		Shape savedClip = g2d.getClip();
		g2d.clip(new Float(1, 1, w - 2, h - 2, arc, arc));
		g2d.drawImage(blurImage, 0, 0, null);
		g2d.setColor(RThemeColor.colorSet.transBGColor);
		g2d.fillRect(0, 0, w, h);
		g2d.setClip(savedClip);
		g2d.setColor(RThemeColor.windowBorderColor);
		g2d.drawRoundRect(1, 1, w - 2, h - 2, arc, arc);
		RenderingConfig.resetAAPainting(g2d, oldRenderingHints);
	}

	@Override
	public Popup getPopup(JPopupMenu pop, int x, int y)
	{
		if (pop instanceof IPopupMenu)
			((IPopupMenu) pop).changLanguage();
		updateFont(pop);
		Dimension dim = pop.getPreferredSize();
		if (dim.width <= 0 || dim.height <= 0)
			return super.getPopup(pop, x, y);
		Rectangle rect = new Rectangle(x, y, dim.width, dim.height);
		clearImage = ScreenTools.captureImage(rect);
		blurImage = ImageTools.getBlurImage(clearImage, 10);
		return super.getPopup(pop, x, y);
	}

	private void updateFont(JPopupMenu pop)
	{
		if (fontIdentifier != FontTools.fontIdentifier)
		{
			for (Component c : pop.getComponents())
				FontTools.customFontSetting(c);
			fontIdentifier = FontTools.fontIdentifier;
		}
	}
}
